package guoan.web.weixin;

import guoan.core.common.WebConfig;
import guoan.core.dto.RestResponse;
import guoan.domain.Exam;
import guoan.domain.ExamConfig;
import guoan.domain.Student;
import guoan.service.ExamConfigService;
import guoan.service.ExamItemService;
import guoan.service.ExamService;
import guoan.service.StudentService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Splitter;

@Controller
@RequestMapping("/weixin/exam")
public class WeiXinExamController {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private ExamService examService;
	@Autowired
	private ExamItemService examItemService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ExamConfigService examConfigService;

	/** 跳转到预约考试列表 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) == null) {
			return "redirect:/weixin/toExam";
		}
		List<Exam> exams = examService.findByResTimeLessThanAndExamTimeGreaterThanEqual();
		model.addAttribute("exams", exams);
		ExamConfig examConfig = examConfigService.findExamConfig();
		model.addAttribute("examConfig", examConfig);
		return "/weixin/exam/index";
	}

	/** 跳转到加入预约考试 */
	@RequestMapping(value = "join/{id}", method = RequestMethod.GET)
	public String toJoin(@PathVariable("id") Long id, HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) == null) {
			return "redirect:/weixin/toExam";
		}

		Exam exam = examService.findByIdAndResTimeLessThanAndExamTimeGreaterThanEqual(id);
		if (exam == null) {
			model.addAttribute("msg", "您所选择的预约模拟当前已不开放报名");
			return "/weixin/msg";
		}
		String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
		Student student = studentService.findByStudentOpenid(openid);
		model.addAttribute("examRanges", Splitter.on(",").split(exam.getOpenHours()).iterator());
		model.addAttribute("exam", exam);
		model.addAttribute("student", student);
		return "/weixin/exam/joinExam";
	}

	@RequestMapping(value = "join", method = RequestMethod.POST)
	public String join(Long examId, Long openHours, Student student, String randomKey, String carType,
			HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		if (session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY) == null) {
			return "redirect:/weixin/toExam";
		}
		if (examId == null || openHours == null || student == null) {
			model.addAttribute("msg", "参数错误");
			return "/weixin/msg";
		} else {
			String openid = (String) session.getAttribute(WebConfig.SESSION_WEIXIN_OPENID_KEY);
			student.setStudentOpenid(openid);
			RestResponse<Boolean> rr;
			try {
				rr = examItemService.joinExamItem(student, examId, openHours, randomKey, carType);
				if (rr.getStatus() == 1) {// 正确
					model.addAttribute("msg", "报名成功，将跳转到您的预约信息界面");
					model.addAttribute("url", "/weixin/student/examItem");
					return "/weixin/msg";
				} else {
					model.addAttribute("msg", rr.getMsg());
					return "/weixin/msg";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				model.addAttribute("msg", "当前预约的人员过多，请过段时间尝试。谢谢。");
				return "/weixin/msg";
			}
		}
	}
}
